/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.mybatis.model;

import java.util.List;
import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;

/**
 *
 * @author ProjetGWTPhp
 */
public class ComboBoxModelTable  extends AbstractListModel implements ComboBoxModel {
    /*
     public static final String DATABASE_USER = "user";

     public static final String DATABASE_PASSWORD = "password";

     public static final String MYSQL_AUTO_RECONNECT = "autoReconnect";

     public static final String MYSQL_MAX_RECONNECTS = "maxReconnects"; 
     */

    private DataBaseTable selectedTable;
    private List<DataBaseTable> listTable;
    

    public ComboBoxModelTable(List<DataBaseTable> listTable) {
        this.listTable = listTable;
    }

//    public ComboBoxModelJDBCConnection() {
//        org.netbeans.modules.db.explorer.ConnectionList f = org.netbeans.modules.db.explorer.ConnectionList.getDefault();
//        org.netbeans.modules.db.explorer.DatabaseConnection[] connections = f.getConnections();
//        this.listTable=new DBConnection[connections.length];
//        for (int x = 0; connections.length > x; x++) {
//            this.listTable[x]= new DBConnection(connections[x]);
//        }
//    }

    @Override
    public DataBaseTable getSelectedItem() {
        if (selectedTable == null) {
            return null;
        }
        return selectedTable;
    }

    @Override
    public void setSelectedItem(Object newValue) {
        if (newValue instanceof DBConnection) {
            for (DataBaseTable o : listTable) {

                if (((DataBaseTable) newValue).getNomeTable().equals(o.getNomeTable())) {
                    selectedTable = o;
                    fireContentsChanged(this, -1, -1);
                    break;
                }
            }
        } else {
            for (DataBaseTable o : listTable) {

                if (String.valueOf(newValue).equals(o.getNomeTable())) {
                    selectedTable = o;
                    //fireContentsChanged(this, -1, -1);
                    break;
                }
            }
        }
    }


    @Override
    protected void fireContentsChanged(Object source, int index0, int index1) {
        super.fireContentsChanged(source, index0, index1); //To change body of generated methods, choose Tools | Templates.
    }
    @Override
    public int getSize() {
        return listTable.size();
    }

    @Override
    public String getElementAt(int i) {
        return listTable.get(i).getNomeTable();

    }
    
}